'use strict';
module.exports = {
	/**
	 * 新增意见反馈
	 * @url client/feedback/kh/add 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, db, _ } = util;
		let { uid, suggestArea, contactInfo } = data;
		let res = { code: 0, msg: "" };
		// 业务逻辑开始-----------------------------------------------------------

		let info = await vk.baseDao.findById({
			dbName: "uni-id-users",
			id: uid
		});

		let dbName = "uce-feedback";
		const transaction = await vk.baseDao.startTransaction();
		try {
			await vk.baseDao.add({
				dbName,
				dataJson: {
					uid: uid,
					user_name: info.nickname,
					contact: contactInfo,
					content: suggestArea
				}
			});
			await transaction.commit();
			return {
				code: 0,
				msg: "提交成功"
			}
		} catch (err) {
			// 事务回滚
			await transaction.rollback();
			console.error(`transaction error`, err)
			return {
				code: -1,
				msg: "提交失败",
				err: {
					message: err.message,
					stack: err.stack
				}
			}
		}


		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}
